import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from common.Utils import ExamDriver

#测试提交考试
class ExamSubmit:
    url = ""
    driver = ""
    def __init__(self):
        self.driver = ExamDriver.driver
        self.url = "https://gms.test.tgtrain.cn/exam/detail?id=17256&did=25296"
        self.driver.get(self.url)
        self.driver.implicitly_wait(10)
        # 点击开始考试
        self.driver.find_element(By.CSS_SELECTOR, "#app > section > section.mod-1 > div.content > div.bottom > div.btn > button").click()

    #已登录
    def ExamSubLoginSuc(self):
        self.driver.implicitly_wait(10)
        # 1.未完成题目提交
        #进入考试后给予3秒时间缓冲
        time.sleep(3)
        # 添加显示等待
        element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div > div > div.ant - modal - wrap > div > div.ant - modal - content > div > div > div.ant - modal - confirm - btns > button")))
        element.click()
        #点击提交
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > div > section.main > div > div.sidebar > div > div.sheet > div.btn > button:nth-child(2)").click()

        # 取提示框出现词语
        element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div:nth-child(9) > div > div.ant-modal-wrap > div > div.ant-modal-content > div > div > div.ant-modal-confirm-body > span.ant-modal-confirm-title")))
        tip = element.text  # 获取元素的文本内容

        # 添加截图
        ExamDriver.getScreeShot()

        assert tip == '还有5道试题未答确定要提交试卷吗？'

        #点击确定
        self.driver.find_element(By.CSS_SELECTOR, "body > div:nth-child(9) > div > div.ant-modal-wrap > div > div.ant-modal-content > div > div > div.ant-modal-confirm-btns > button.ant-btn.ant-btn-primary").click()
        #查看是否回到了考试详情页
        self.driver.find_element(By.CSS_SELECTOR, "#app > section > section.mod-1 > div.content > div.bottom > div.btn > button")

        # 2.完成所有题目提交
        #点击开始考试
        self.driver.find_element(By.CSS_SELECTOR, "#app > section > section.mod-1 > div.content > div.bottom > div.btn > button").click()

        # 添加显示等待
        element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div > div > div.ant - modal - wrap > div > div.ant - modal - content > div > div > div.ant - modal - confirm - btns > button")))
        element.click()

        # 10秒手动完成所有题目
        time.sleep(10)

        # 点击提交
        self.driver.find_element(By.CSS_SELECTOR, "#app > div > div > section.main > div > div.sidebar > div > div.sheet > div.btn > button:nth-child(2)").click()

        # 取提示框出现词语
        element = WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "body > div:nth-child(9) > div > div.ant-modal-wrap > div > div.ant-modal-content > div > div > div.ant-modal-confirm-body > span.ant-modal-confirm-title")))
        tip = element.text  # 获取元素的文本内容

        # 添加截图
        ExamDriver.getScreeShot()

        assert tip == '确定要提交试卷吗？'
        # 点击确定
        self.driver.find_element(By.CSS_SELECTOR, "body > div:nth-child(9) > div > div.ant-modal-wrap > div > div.ant-modal-content > div > div > div.ant-modal-confirm-btns > button.ant-btn.ant-btn-primary").click()
        # 查看是否回到了考试详情页

        self.driver.find_element(By.CSS_SELECTOR, "#app > section > section.mod-1 > div.content > div.bottom > div.btn > button")